package edu.whu.powersoap;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import com.baidu.location.BDLocation;
import com.baidu.mapapi.cloud.CloudListener;
import com.baidu.mapapi.cloud.CloudSearchResult;
import com.baidu.mapapi.cloud.DetailSearchResult;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.model.LatLngBounds;

public class JobFinishedDetailActivity extends Activity implements CloudListener{
	private final static String TAG = "JobFinishedDetailActivity";
	public Intent intent ;
	public String number;
	public ContentValues values;
	private Context mContext;
	private MapView mMapView;
	private String mPoi;
	private BDLocation mLocation;
	private BaiduMap mfBaiduMap = JobDetailActivity.mBaiduMap;
	protected void onCreate(Bundle savedInstanceState) {
		mContext = JobFinishedDetailActivity.this;
		intent = getIntent();
		number = intent.getExtras().getString("number");
		Log.i(TAG, "number=" + number);

		ContentResolver cr = mContext.getContentResolver();
		Cursor cur = cr.query(JobFinishedProvider.CONTENT_URI, new String[] {
				JobFinishedProvider.VOLUMN_NUMBER + " as _id",
				JobFinishedProvider.VOLUMN_CREATE_TIME, 
				JobFinishedProvider.VOLUMN_FINISH_TIME,
				JobFinishedProvider.VOLUMN_EXECUTOR,
				JobFinishedProvider.VOLUMN_POI_LIST}, "number=?",
				new String[] { number }, null);
		String c_time= null;
		String f_time = null;
		String poi = null;
		String executor = null;
		if (cur != null) {
			if (cur.moveToFirst()) {
				// do {
				c_time = cur.getString(1);
				f_time = cur.getString(2);
				executor = cur.getString(3);
				poi = cur.getString(4);
				// } while (c.moveToNext());
			}
			cur.close();
		}
		Log.i(TAG, "poi=" + poi + ", executor=" + executor);
		mPoi = poi;
		setContentView(R.layout.job_finished_detail);

		TextView tv = (TextView) findViewById(R.id.textView1);
		tv.setText(tv.getText() + number);
		tv = (TextView) findViewById(R.id.textView2);
		tv.setText(tv.getText() + executor);
		tv = (TextView) findViewById(R.id.textView3);
		tv.setText(tv.getText() + c_time);
		tv = (TextView) findViewById(R.id.textView4);
		tv.setText(tv.getText() + f_time);
		tv = (TextView) findViewById(R.id.textView5);
		tv.setText(tv.getText() + poi);
		
		//mark provider
		Cursor curm = cr.query(MarkProvider.CONTENT_URI, new String[] {
				MarkProvider.VOLUMN_NUMBER + " as _id",
				MarkProvider.VOLUMN_FLAG,
				MarkProvider.VOLUMN_WORD,
				MarkProvider.VOLUMN_PHOTO_PATH,				
				MarkProvider.VOLUMN_VIDEO_PATH,
				MarkProvider.VOLUMN_VOICE_PATH
				}, "number=?",
				new String[] { number }, null);
		String sta = null;
		String wor = null;
		String pho = null;
		String vid = null;
		String voi = null;
		if (curm != null) {
			if (curm.moveToFirst()) {
				// do {
				sta = curm.getString(1);
				wor = curm.getString(2);
				pho = curm.getString(3);
				vid = curm.getString(4);
				voi = curm.getString(5);
				// } while (c.moveToNext());
			}
			curm.close();
		}
//		setContentView(R.layout.job_finished_detail);
		if(sta == null){
			sta = "unmarked";
		}
		if(wor == null){
			wor = "no record";
		}
		if(pho == null){
			pho = "no record";
		}
		if(vid == null){
			vid = "no record";
		}
		if(voi == null){
			voi = "no record";
		}
		TextView tvm = (TextView) findViewById(R.id.textView6);
		tvm.setText(tvm.getText() + sta);
		tvm = (TextView) findViewById(R.id.textView7);
		tvm.setText(tvm.getText() + wor);
		tvm = (TextView) findViewById(R.id.textView8);
		tvm.setText(tvm.getText() + pho);
		tvm = (TextView) findViewById(R.id.textView9);
		tvm.setText(tvm.getText() + vid);
		tvm = (TextView) findViewById(R.id.textView10);
		tvm.setText(tvm.getText() + voi);
		
		
		mMapView = (MapView) findViewById(R.id.mapView);
		mfBaiduMap = mMapView.getMap();
		
//		BitmapDescriptor bd = BitmapDescriptorFactory
//				.fromResource(R.drawable.icon_gcoding);
//		LatLng ll;
//		LatLngBounds.Builder builder = new LatLngBounds.Builder();
//		String[] pos = mPoi.split(",");
//		Log.i(TAG, "pos0=" + pos[0] + ", pos1=" + pos[1]);
//		ll = new LatLng(Double.parseDouble(pos[0]),
//				Double.parseDouble(pos[1]));
//		OverlayOptions oo = new MarkerOptions().icon(bd).position(ll);
//		mfBaiduMap.addOverlay(oo);
//		builder.include(ll);
//
//		ll = new LatLng(mLocation.getLatitude(), mLocation.getLongitude());
//		builder.include(ll);
//
//		LatLngBounds bounds = builder.build();
//		MapStatusUpdate u = MapStatusUpdateFactory.newLatLngBounds(bounds);
//		mfBaiduMap.animateMapStatus(u);
		
		super.onCreate(savedInstanceState);
		}
	@Override
	public void onGetDetailSearchResult(DetailSearchResult arg0, int arg1) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onGetSearchResult(CloudSearchResult arg0, int arg1) {
		// TODO Auto-generated method stub
		
	}
	

}
